package com.city.controller.auth;

import com.city.domain.auth.AuthLevelOne;
import com.city.domain.auth.AuthLevelPub;
import com.city.domain.auth.AuthLevelThree;
import com.city.domain.auth.AuthLevelTwo;
import com.city.service.auth.AuthLevelOneService;
import com.city.service.auth.AuthLevelPubService;
import com.city.service.auth.AuthLevelThreeService;
import com.city.service.auth.AuthLevelTwoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/auth")
public class AuthController {

    @Autowired
    private AuthLevelOneService oneService;

    @Autowired
    private AuthLevelTwoService twoService;

    @Autowired
    private AuthLevelThreeService threeService;

    @Autowired
    private AuthLevelPubService authLevelPubService;

    /**
     * @author  王宇
     * @date 2018/8/31
     * @description  权限管理页面
     */
    @RequestMapping("/manager")
    public String manger(){

        return "/auth/manger";
    }

    /**
     * @author  王宇
     * @date 2018/8/31
     * @description  登录验证
     */
    @RequestMapping("/login")
    public String login(Map<String,Object> map){

        return "/auth/login";
    }

    /**
     * @author  王宇
     * @date 2018/9/1
     * @description  错误跳转页面
     */
    @RequestMapping("/error")
    public String error(Map<String,Object> map,
                        @Param("url") String url,
                        @Param("msg") String msg,
                        @Param("type") int type){

        map.put("url",url);
        map.put("msg",msg);
        map.put("type",type);
        System.out.println(msg);
        return "/auth/error";
    }


    @RequestMapping("/index")
    public String index(Map<String,Object> map,
                        HttpServletResponse response,
                        HttpSession session) throws IOException {
        map.put("title","城乡综合信息管理后台");
        //判断是否登录
        if(session.getAttribute("user")==null){
            response.sendRedirect("/auth/error?url=/auth/login"+"&type=2&msg="+ URLEncoder.encode("没有登录，正在跳转登陆", "UTF-8"));
        }

        return "/auth/index";
    }

    @RequestMapping("/branchlist/index")
    public String branchlist(Map<String,Object> map,
                             HttpServletResponse response,
                             HttpSession session) throws Exception{
        map.put("title","部门列表");
        //判断是否登录
        if(session.getAttribute("user")==null){
            response.sendRedirect("/auth/error?url=/auth/login"+"&type=2&msg="+ URLEncoder.encode("没有登录，正在跳转登陆", "UTF-8"));
        }
        return "/auth/branchlist";
    }

    /**
     * @author  王宇
     * @date 2018/9/1
     * @description  权限管理页面
     */
    @RequestMapping("/authmanager/index")
    public String authmanager(Map<String,Object> map,
                              HttpServletResponse response,
                              HttpSession session) throws Exception{
        map.put("title","权限管理");
        //判断登录
        if(session.getAttribute("user")==null){
            response.sendRedirect("/auth/error?url=/auth/login"+"&type=2&msg="+ URLEncoder.encode("没有登录，正在跳转登陆", "UTF-8"));
        }

        return "/auth/authmanager";
    }

    /**
     * @author  王宇
     * @date 2018/9/1
     * @description  添加权限界面
     */
    @RequestMapping("/addauth/index")
    public String addauth(Map<String,Object> map,
                          HttpServletResponse response,
                          HttpSession session) throws Exception {
        //判断登录
        if(session.getAttribute("user")==null){
            response.sendRedirect("/auth/error?url=/auth/login"+"&type=2&msg="+ URLEncoder.encode("没有登录，正在跳转登陆", "UTF-8"));
        }


        List<AuthLevelOne> levelOnes = oneService.findAll();
        List<AuthLevelTwo> levelTwos = twoService.findAll();
        List<AuthLevelThree> levelThrees = threeService.findAll();


        map.put("title","添加权限");

        map.put("levelOne",levelOnes);

        map.put("levelTwo",levelTwos);

        map.put("levelThree",levelThrees);
        return "/auth/addauth";
    }

    /**
     * @author  王宇
     * @date 2018/9/1
     * @description  修改权限
     */
    @RequestMapping("/updateauth/index")
    public String updateauth(Map<String,Object> map,
                             HttpServletResponse response,
                             HttpSession session) throws Exception{
        //判断登录
        if(session.getAttribute("user")==null){
            response.sendRedirect("/auth/error?url=/auth/login"+"&type=2&msg="+ URLEncoder.encode("没有登录，正在跳转登陆", "UTF-8"));
        }

        List<AuthLevelOne> levelOnes = oneService.findAll();
        List<AuthLevelTwo> levelTwos = twoService.findAll();
        List<AuthLevelThree> levelThrees = threeService.findAll();




        map.put("title","修改权限");

        map.put("levelOne",levelOnes);

        map.put("levelTwo",levelTwos);

        map.put("levelThree",levelThrees);

        return "/auth/updateauth";
    }

    /**
     * @author  王宇
     * @date 2018/10/6
     * @description  人员列表
     */
    @RequestMapping("/branchpeople/index")
    public String personlist(Map<String,Object> map,
                             HttpServletResponse response,
                             HttpSession session) throws Exception{
        //判断登录
        if(session.getAttribute("user")==null){
            response.sendRedirect("/auth/error?url=/auth/login"+"&type=2&msg="+ URLEncoder.encode("没有登录，正在跳转登陆", "UTF-8"));
        }
        return "/auth/personlist";
    }

    /**
     * @Author: 李顺时
     * @Description: 部门人员管理添加
     * @Date: 14:15 2018/10/6
     */
    @RequestMapping("/branchpeople/addperson")
    public String addpersonlist(Map<String,Object> map,
                             HttpServletResponse response,
                             HttpSession session) throws Exception{
        //判断登录
        if(session.getAttribute("user")==null){
            response.sendRedirect("/auth/error?url=/auth/login"+"&type=2&msg="+ URLEncoder.encode("没有登录，正在跳转登陆", "UTF-8"));
        }
        return "/auth/addpersonlist";
    }

}
